var headurl = `https://techcherry.cn/lctapi/`
//`https://bwv.net.cn/${__wxConfig.envVersion == 'trial' ? 'prod' :'api'}`;


 

var imgurl = `${headurl}imgs/`, audiourl = `${headurl}audio/`, videourl = `${headurl}video/`
class api {

  getheadurl() {
    return headurl;
  }

  getimgurl() {
    return imgurl;
  }
  getaudiourl() {
    return audiourl;
  }
  getvideourl() {
    return videourl;
  }
  /**
   * 登录
   * code- string, 小程序调用login生成的code(临时登录凭证)
   */
  wxlogin(code, success, fail) {
    fstpostdata('/login/wxlogin', {
      jscode: code
    }, res => success(res), res => fail(res))
  }
  
  
  uploadimg(data, params) {
    console.log(data,params,"q11fesfds");
    // return;
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0, //上传失败的个数
      pics = data.pics ? data.pics : [];
      if(data.path[i].indexOf(imgurl)!=-1){
        pics.push(data.path[i].replace(imgurl,''));
      }
      // data.path.forEach(x=>{
      //   if(x.indexOf(imgurl)!=-1){
      //     pics.push(x.replace(imgurl,''));
      //   }
      // })
    console.log(data.path[i],'data.path[i]');
    wx.uploadFile({
      header: {
        'content-type': "application/x-www-form-urlencoded",
        token: getApp().logininfo.Token,
        userid: getApp().logininfo.userid
      },
      url: data.url,
      filePath: data.path[i],
      name: 'upload', //这里根据自己的实际情况改
      formData: null, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        let result = JSON.parse(resp.data);
        console.log(result.path,'result.pathresult.path');
        pics.push(result.path);
        console.log(pics, "picssss")
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1 
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log(res,'fail--result.pathresult.path');
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);

        i++; //这个图片执行完上传后，开始上传下一张            
        if (i == data.path.length) { //当图片传完时，停止调用          
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
          params.success(pics);

        } else { //若图片还没有传完，则继续调用函数                
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          data.pics = pics;
          that.uploadimg(data, params);
        }
      }
    });
  }

  uploadvideo(data, params) {
    console.log(data,params,"q11fesfds");
    // return;
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0, //上传失败的个数
      pics = data.pics ? data.pics : [];
      if(data.path[i].indexOf(videourl)!=-1){
        pics.push(data.path[i].replace(videourl,''));
      }
      // data.path.forEach(x=>{
      //   if(x.indexOf(imgurl)!=-1){
      //     pics.push(x.replace(imgurl,''));
      //   }
      // })
    console.log(data.path[i],'data.path[i]');
    wx.uploadFile({
      header: {
        'content-type': "application/x-www-form-urlencoded",
        token: getApp().logininfo.Token,
        userid: getApp().logininfo.userid
      },
      url: data.url,
      filePath: data.path[i],
      name: 'upload', //这里根据自己的实际情况改
      formData: null, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        let result = JSON.parse(resp.data);
        console.log(result.path,'result.pathresult.path');
        pics.push(result.path);
        console.log(pics, "picssss")
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1 
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log(res,'fail--result.pathresult.path');
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++; //这个图片执行完上传后，开始上传下一张            
        if (i == data.path.length) { //当图片传完时，停止调用          
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
          params.success(pics);
        } else { //若图片还没有传完，则继续调用函数                
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          data.pics = pics;
          that.uploadvideo(data, params);
        }
      }
    });
  }

// liu
// 小程序获取所有二级分类列表
getSettings(succ) {
  postdata('/Base/getSettings', {}, res => succ(res))
}


// liu end

  prepay(data, succ, fail) {
    // data['openid'] = wx.getStorageSync('openid')
    postdata('/Orders/pay', data, res => {
      var data = JSON.parse(res.data.res);
      console.log(res,'res');
      var orderid = res.data.ordernumeber;
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonce_str,
        package: data.package,
        paySign: data.paySign,
        signType: 'MD5',
        success: res => {
          console.log(res, 'succ')
          if (res.errMsg.indexOf('ok') > -1) {
            succ && succ({ succ: 1, orderid})
          }
        },
        fail: res => {
          //支付取消
          succ && succ({succ:-1,orderid})
        }
      })
    })
  }
}




function postpyqdata(urlparms, data, success, fail) {
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function postdata(urlparms, data, success, fail) {
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
      token: getApp().logininfo.Token,
      userid: getApp().logininfo.userid
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function getdata(urlparms, data, success, fail) {

  wx.request({
    url: headurl + urlparms,
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
      'userId': getApp().userinfo.userId,
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function fstpostdata(urlparms, data, success, fail) {
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}



module.exports = api